import 'package:clean_app/asset/color.dart';
import 'package:clean_app/asset/modal.dart';
import 'package:clean_app/asset/textstyle.dart';
import 'package:clean_app/outer/bounding_box_annotation/models/label.dart';
import 'package:clean_app/provider/task_provider.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:provider/provider.dart';

class AnnotationLabelDialog extends StatefulWidget {
  final String header;
  final Label label;
  const AnnotationLabelDialog({
    super.key,
    required this.header,
    required this.label,
  });

  @override
  State<AnnotationLabelDialog> createState() => _AnnotationLabelDialogState();
}

class _AnnotationLabelDialogState extends State<AnnotationLabelDialog> {
  final _formKey = GlobalKey<FormState>();
  final _controller = TextEditingController();

  late Label label;

  @override
  void initState() {
    getPresetTags();
    setState(() {
      _controller.text = widget.label.remark;
      label = widget.label;
    });
    super.initState();
  }

  getPresetTags() async {
    var _p = Provider.of<PresetTagProvider>(context, listen: false);
    try {
      if (_p.presetTags.isNotEmpty) {
        candidateOptions =
            _p.presetTags.map((e) => e["tag_name"].toString()).toList();
      }
    } catch (_) {}
    setState(() {});
  }

  List<String> candidateOptions = ["污渍", "纸屑", "纸团", "脏污"];
  String selectedOption = "";

  @override
  Widget build(BuildContext context) {
    return Dialog(
      shape: const RoundedRectangleBorder(
        borderRadius: BorderRadius.all(Radius.circular(10.0)),
      ),
      child: Container(
        constraints: const BoxConstraints(maxWidth: 400.0),
        padding: EdgeInsets.only(left: 15, top: 15, right: 15, bottom: 15),
        child: Form(
          key: _formKey,
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            mainAxisSize: MainAxisSize.min,
            children: <Widget>[
              Wrap(
                spacing: 10,
                children: [
                  ...List.generate(candidateOptions.length, (res) {
                    var i = candidateOptions[res];
                    return ElevatedButton(
                      style: ElevatedButton.styleFrom(
                        elevation: 0,
                        backgroundColor:
                            i == label.tag ? os_color : Colors.white,
                        foregroundColor:
                            i == label.tag ? Colors.white : Colors.black,
                        padding: EdgeInsets.only(left: 15, right: 15),
                      ),
                      onPressed: () {
                        label.tag = i;
                        selectedOption = i;
                        setState(() {});
                      },
                      child: Text(
                        "${res + 1}.$i ",
                        style: XSTextStyle(
                          context: context,
                          // color: os_black,
                          fontSize: 15,
                          fontWeight: FontWeight.bold,
                        ),
                      ),
                    );
                  }),
                  // for (var i in candidateOptions)
                  ElevatedButton(
                    style: ElevatedButton.styleFrom(
                      elevation: 0,
                      backgroundColor: Colors.white,
                      foregroundColor: Colors.black,
                    ),
                    onPressed: () {
                      showInputModal(
                        context: context,
                        title: "请输入标签名称",
                        confirm: (val) {
                          setState(() {
                            candidateOptions.add(val);
                            label.tag = val;
                            selectedOption = val;
                          });
                        },
                      );
                    },
                    child: Text(
                      "+",
                      style: XSTextStyle(
                        context: context,
                        color: os_deep_grey,
                        fontSize: 18,
                        fontWeight: FontWeight.normal,
                      ),
                    ),
                  ),
                ],
              ),
              SizedBox(height: 2.5),
              Row(
                children: [
                  Text(
                    " 评分: ",
                    style: XSTextStyle(
                      context: context,
                      color: os_color,
                      fontSize: 14,
                      fontWeight: FontWeight.bold,
                    ),
                  ),
                  Text(
                    label.score.toInt().toString(),
                    style: XSTextStyle(
                      context: context,
                      color: os_color,
                      fontSize: 14,
                      fontWeight: FontWeight.bold,
                    ),
                  ),
                  Expanded(
                    child: Slider(
                      min: 0,
                      max: 100,
                      activeColor: os_color,
                      inactiveColor: os_middle_grey,
                      value: label.score.toDouble(),
                      onChanged: (val) {
                        label.score = val.ceil();
                        setState(() {});
                      },
                    ),
                  ),
                ],
              ),
              SizedBox(height: 5),
              TextFormField(
                style: const TextStyle(fontSize: 16.0),
                cursorColor: os_color,
                decoration: InputDecoration(
                  filled: true,
                  fillColor: Colors.white,
                  border: OutlineInputBorder(
                    borderRadius: BorderRadius.circular(10),
                    borderSide: BorderSide(color: os_color, width: 2),
                  ),
                  focusedBorder: OutlineInputBorder(
                    borderRadius: BorderRadius.circular(10),
                    borderSide: BorderSide(color: os_color, width: 2),
                  ),
                  hintText: "请输入备注信息",
                  hintStyle: TextStyle(
                    color: Color(0xFFA9A9A9),
                  ),
                  contentPadding: EdgeInsets.symmetric(
                    vertical: 10.0,
                    horizontal: 15.0,
                  ),
                ),
                inputFormatters: [LengthLimitingTextInputFormatter(15)],
                validator: (value) {
                  if (value!.isEmpty) {
                    return "Enter Annotation Label";
                  }
                  return null;
                },
                controller: _controller,
                onChanged: (_val) {
                  setState(() {
                    label.remark = _val;
                  });
                },
              ),
              SizedBox(height: 15.0),
              Row(
                mainAxisAlignment: MainAxisAlignment.end,
                children: [
                  Expanded(
                    child: TextButton(
                      onPressed: () {
                        Navigator.of(context).pop();
                      },
                      style: ButtonStyle(
                        backgroundColor: WidgetStatePropertyAll(os_color_opa),
                      ),
                      child: Padding(
                        padding: EdgeInsets.symmetric(
                          vertical: 5.0,
                          horizontal: 5.0,
                        ),
                        child: Text(
                          "取消",
                          style: TextStyle(
                            color: os_color,
                            fontWeight: FontWeight.w600,
                          ),
                        ),
                      ),
                    ),
                  ),
                  const SizedBox(width: 10.0),
                  Expanded(
                    child: TextButton(
                      onPressed: () {
                        // 检查tag、remark、score
                        if (label.tag.isEmpty) {
                          Toast.showInCenter(text: "请填写标签", context: context);
                          return;
                        }
                        // if (label.remark.isEmpty) {
                        //   Toast.showInCenter(
                        //       text: "请填写备注信息", context: context);
                        //   return;
                        // }
                        if (label.score == 0) {
                          Toast.showInCenter(text: "请选择分数", context: context);
                          return;
                        }
                        Navigator.of(context).pop(label);
                      },
                      style: ButtonStyle(
                        backgroundColor: WidgetStatePropertyAll(os_color),
                      ),
                      child: Padding(
                        padding: EdgeInsets.symmetric(
                          vertical: 5.0,
                          horizontal: 5.0,
                        ),
                        child: Text(
                          "确认",
                          style: TextStyle(
                            fontWeight: FontWeight.w600,
                            color: os_white,
                          ),
                        ),
                      ),
                    ),
                  ),
                ],
              ),
            ],
          ),
        ),
      ),
    );
  }
}
